# Paper: Is it still the economy? Economic voting in polarized politics
# Individual-level simulation
# Replication: Figure 3 (and Figures S19 and S20 in theSupplementary Material)
remove(list = ls())

library(conflicted)
library(tidyverse)
library(dplyr)
conflict_prefer("filter","dplyr")
library(ggplot2)
library(here) # When we have an R-project, it reads files without setting an directory
conflict_prefer("here", "here")
library(ggeffects)

load(here("Final_Paper", "R&R", "ElitePolarization_Series.RData"))

fig1 <- polar_data %>% 
  mutate(party = as.factor(party)) %>% 
  ggplot(aes(x = year, y = median_voter, color = party, shape = party)) +
  geom_vline(xintercept = 1991, linetype = "dashed", color = "gray37", size = 1) +
  geom_point(size = 2.5) + 
  geom_line(size = 1) + theme_light() +
  ylab("Ideological Position") + xlab(NULL) +
  #geom_hline(yintercept = 0, color = "gray37", lty = 2, size = 1) +
  scale_color_manual(name = "Median Representative:", values = c("blue", "red"), labels = c("Dem", "Rep")) +
  scale_shape_manual(name = "Median Representative:", values = c(19, 17), labels = c("Dem", "Rep")) +
  scale_y_continuous(breaks = c(-0.5, -0.3, -0.1, 0.1, 0.3, 0.5),
                     limits = c(-0.55, 0.55)) +
  scale_x_continuous(breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2020)) +
  theme(legend.position = "bottom",
        axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 14),
        axis.title.x = element_text(size = 14),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 15)) 
fig1


fig2 <- polar_data %>% 
  filter(year > 1989) %>% 
  filter(year < 2021) %>% 
  mutate(party = as.factor(party)) %>% 
  ggplot(aes(x = year, y = median_voter, color = party, shape = party)) +
  #geom_vline(xintercept = 1991, linetype = "dashed", color = "gray37", size = 1) +
  geom_point(size = 2.5) + 
  geom_line(size = 1) + theme_light() +
  ylab("Ideological Position") + xlab(NULL) +
  #geom_hline(yintercept = 0, color = "gray37", lty = 2, size = 1) +
  scale_color_manual(name = "Median Representative:", values = c("blue", "red"), labels = c("Dem", "Rep")) +
  scale_shape_manual(name = "Median Representative:", values = c(19, 17), labels = c("Dem", "Rep")) +
  scale_y_continuous(breaks = c(-0.5, -0.3, -0.1, 0.1, 0.3, 0.5),
                     limits = c(-0.55, 0.55)) +
  scale_x_continuous(breaks = c(1992, 1996, 2000, 2004, 2008,
                                2012, 2016)) +
  theme(legend.position = "bottom",
        axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 14),
        axis.title.x = element_text(size = 14),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 15)) 
fig2

fig3 <- polar_data %>% 
  filter(year < 1991) %>% 
  mutate(party = as.factor(party)) %>% 
  ggplot(aes(x = year, y = median_voter, color = party, shape = party)) +
  #geom_vline(xintercept = 1991, linetype = "dashed", color = "gray37", size = 1) +
  geom_point(size = 2.5) + 
  geom_line(size = 1) + theme_light() +
  ylab("Ideological Position") + xlab(NULL) +
  #geom_hline(yintercept = 0, color = "gray37", lty = 2, size = 1) +
  scale_color_manual(name = "Median Representative:", values = c("blue", "red"), labels = c("Dem", "Rep")) +
  scale_shape_manual(name = "Median Representative:", values = c(19, 17), labels = c("Dem", "Rep")) +
  scale_y_continuous(breaks = c(-0.5, -0.3, -0.1, 0.1, 0.3, 0.5),
                     limits = c(-0.55, 0.55)) +
  scale_x_continuous(breaks = c(1960, 1964, 1968, 1972, 1976,
                                1980, 1984, 1988)) +
  theme(legend.position = "bottom",
        axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 14),
        axis.title.x = element_text(size = 14),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 15)) 
fig3

